Dropout

训练时对输入做 Dropout:按掩码将部分元素置零,并对保留元素缩放。对每个元素:

\[output_i = input_i \cdot mask_i \cdot scale\]

其中 mask 元素为 0 或 1,scale 通常取 1/(1-p)(p 为丢弃概率)。 掩码由调用方预先生成;算子本身不采样。

输入:
  • input - 输入数据地址

  • scale - 缩放因子(标量)

  • length - 元素个数

  • mask - 掩码地址,长度 length

  • core_mask - 核掩码(仅共享存储版本)

输出:
  • output - 输出地址,长度 length

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持 fp32

  • MT7004 支持 fp16、fp32

共享存储版本:

void hp_dropout_s(float16 *input, float16 scale, int length, float16 *output, float16 *mask, int core_mask)
void fp_dropout_s(float *input, float scale, int length, float *output, float *mask, int core_mask)

C调用示例:

 1// MT7004 示例(共享存储多核,DDR 地址)
 2void TestDropoutSMCFp32(int length, float scale, int core_mask) {
 3    int core_id = get_core_id();
 4    int logic_core_id = GetLogicCoreId(core_mask, core_id);
 5    int core_num = GetCoreNum(core_mask);
 6    float *input = (float *)0x81000000;
 7    float *output = (float *)0x82000000;
 8    float *mask = (float *)0x83000000;
 9    int i;
10    if (logic_core_id == 0) {
11        for (i = 0; i < length; i++) {
12            input[i] = (float)(i % 100) / 10.0f - 5.0f;
13            mask[i] = (i % 2 == 0) ? 1.0f : 0.0f;
14        }
15    }
16    sys_bar(0, core_num);
17    fp_dropout_s(input, scale, length, output, mask, core_mask);
18}
19
20void main() {
21    int length = 1024;
22    float scale = 0.5f;
23    int core_mask = 0b1111;
24    TestDropoutSMCFp32(length, scale, core_mask);
25}

私有存储版本:

void hp_dropout_p(float16 *input, float16 scale, int length, float16 *output, float16 *mask)
void fp_dropout_p(float *input, float scale, int length, float *output, float *mask)

C调用示例:

 1// MT7004 示例(私有存储单核,AM 地址)
 2void TestDropoutAMFp32(int length, float scale) {
 3    float *input = (float *)0x10010000;
 4    float *output = (float *)0x10020000;
 5    float *mask = (float *)0x10030000;
 6    int i;
 7    for (i = 0; i < length; i++) {
 8        input[i] = (float)(i % 100) / 10.0f - 5.0f;
 9        mask[i] = (i % 2 == 0) ? 1.0f : 0.0f;
10    }
11    fp_dropout_p(input, scale, length, output, mask);
12}
13
14void main() {
15    int length = 1024;
16    float scale = 0.5f;
17    TestDropoutAMFp32(length, scale);
18}